﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Microsoft.MSE.Catalog.SelfHost
{
    /// <summary>
    /// Validates command line arguments.
    /// </summary>
    public class Arguments
    {
        private readonly List<string> _errorMessages = new List<string>();
        private readonly List<string> _warningMessages = new List<string>();
        private readonly List<string> _informationMessages = new List<string>();

        // command line arguments as they came in
        private readonly string[] _arguments;

        // specified sql server argument. could be null.
        private string _sqlServer;

        // specified sql database argument. could be null.
        private string _sqlDatabase;
        
        /// <summary>
        /// Arguments passed in from the command line
        /// </summary>
        /// <param name="arguments">Command line arguments.</param>
        public Arguments(string[] arguments)
        {
            _arguments = arguments;

            if (_arguments != null)
            {
                Parse();
            }
        }

        private void Parse()
        {
            HashSet<string> parsedSwitches = new HashSet<string>();

            foreach (string argument in _arguments)
            {
                // all arguments should start with a slash
                if (!argument.StartsWith("/"))
                {
                    _errorMessages.Add("Invalid argument (" + argument + ") specified.");
                }

                // remove slash
                string[] separatedArgument = argument.Split(':');
                string switchSpecified = separatedArgument[0];
                string switchNormalized = switchSpecified.Remove(0, 1).ToLower();
                string switchValue = null;

                if (separatedArgument.Length > 1)
                {
                    switchValue = separatedArgument[1];
                }

                switch (switchNormalized)
                {
                    case "sqlserver":
                    case "s":

                        if (parsedSwitches.Contains(switchNormalized))
                        {
                            _errorMessages.Add("Switch (/SqlServer) specified more than once.");
                        }

                        _sqlServer = switchValue;
                        _informationMessages.Add("/SqlServer switched used on command line. Using " + _sqlServer + " as SQL Server instance.");
                        break;

                    case "sqldatabase":
                    case "d":

                        if (parsedSwitches.Contains(switchNormalized))
                        {
                            _errorMessages.Add("Switch (/SqlDatabase) specified more than once.");
                        }

                        _sqlDatabase = switchValue;
                        _informationMessages.Add("/SqlDatabase switched used on command line. Using " + _sqlDatabase + " as SQL Server database.");
                        break;

                    default:
                        _errorMessages.Add("Unknown switch (" + switchSpecified + ") specified.");
                        break;
                }

                parsedSwitches.Add(switchNormalized);
            }
        }

        /// <summary>
        /// Error messages. Should cause host to not start.
        /// </summary>
        public List<string> ErrorMessages
        {
            get
            {
                return _errorMessages;
            }
        }

        /// <summary>
        /// Warning messages. Should still start host.
        /// </summary>
        public List<string> WarningMessages
        {
            get
            {
                return _warningMessages;
            }
        }

        /// <summary>
        /// Information messages
        /// </summary>
        public List<string> InfomationMessages
        {
            get
            {
                return _informationMessages;
            }
        }

        /// <summary>
        /// SQL instance where the repository is deployed.
        /// </summary>
        public string SqlServer
        {
            get
            {
                return _sqlServer;
            }
        }

        /// <summary>
        /// Database name of the deployed repository.
        /// </summary>
        public string SqlDatabase
        {
            get
            {
                return _sqlDatabase;
            }
        }
    }
}